package com.bangdream.michelia.view.adapter;


import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.TeacherBean;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.utils.DateUtil;
import com.bangdream.michelia.utils.ImageLoadUtils;
import com.bangdream.michelia.utils.L;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

/**
 * Created by zhouyi on 2018/3/25.
 */

public class ExaminationAdapter extends BaseQuickAdapter<ExamBean, BaseViewHolder> {
    private String type;

    public ExaminationAdapter(int layoutResId, String type) {
        super(layoutResId);
        this.type = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, ExamBean item) {
        ImageLoadUtils.ImageLoad(mContext, item.getFaceUri(), (ImageView) helper.getView(R.id.ivIcon), -1);
        helper.setText(R.id.tvTitle, item.getTitle() + "");
        helper.setText(R.id.tvType, "考试类型：" + (item.getLargeTypeLabel() == null ? "暂无数据" : item.getLargeTypeLabel()) + "");
        helper.setText(R.id.tvDate, "考试时间：" + item.getStartDate() + "始");

        String examIdFinish = item.getExamIdFinish();
        String examId = item.getExamId();

        if (type.equals("1")) {
            helper.setVisible(R.id.ivStatus, false);
            helper.setVisible(R.id.tvState, false);
            helper.setGone(R.id.tvGoExamination, false);
            helper.setVisible(R.id.tvIsFree,true);
        } else {
            helper.setVisible(R.id.tvIsFree,false);
            //已经考试完毕
            if (examIdFinish != null && examIdFinish.length() > 0) {
//            helper.setText(R.id.tvTitle,item.getTitle()+"");
                helper.setVisible(R.id.ivStatus, true);
                helper.setGone(R.id.tvGoExamination, false);
            }
            //正在考试
            else if (examId != null && examId.length() > 0 && (examIdFinish == null || examIdFinish.length() == 0)) {
                helper.setGone(R.id.ivStatus, false);
                helper.setVisible(R.id.tvGoExamination, true);
            } else {
                helper.setVisible(R.id.ivStatus, true);
                helper.setVisible(R.id.tvGoExamination, false);
            }
            if (isPassline(item)) {
                ((ImageView) helper.getView(R.id.ivStatus)).setImageResource(R.mipmap.icon_pass);
            } else {
                ((ImageView) helper.getView(R.id.ivStatus)).setImageResource(R.mipmap.icon_fail);
            }
            setDateState(helper, item);

        }

    }

    //是否及格
    private boolean isPassline(ExamBean bean) {
        boolean isPass = false;
        try {
            //及格线
            int nmPassLine = Integer.parseInt(bean.getPassline());
            //个人平均分
            int nmAvgScore = Integer.parseInt(bean.getAvgScore());
            if (nmAvgScore >= nmPassLine) {
                return true;
            }
        } catch (Exception e) {
            L.e(e.toString());
        }
        return isPass;
    }

    //设置该考试目前的时间状态 未开始 已开始 已结束
    private void setDateState(BaseViewHolder helper, ExamBean bean) {


        String stStartDate = bean.getStartDate();
        String stEndDate = bean.getEndDate();
        DateUtil.DatePattern pattern = DateUtil.DatePattern.ALL_TIME;
        TextView stateView = helper.getView(R.id.tvState);

        try {
            long nmStartDate = DateUtil.stringToDate(stStartDate, pattern).getTime();
            long nmEndDate = DateUtil.stringToDate(stEndDate, pattern).getTime();
            long nmTodayDate = DateUtil.stringToDate(DateUtil.getNowDate(pattern), pattern).getTime();
            //未开始
            if (nmTodayDate < nmStartDate && nmTodayDate < nmEndDate) {
                helper.setText(R.id.tvState, "未开始");

                stateView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.left_label_red_bg));
            } else if (nmTodayDate >= nmStartDate && nmTodayDate < nmEndDate) {
                helper.setText(R.id.tvState, "考试中");
                stateView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.left_label_red_bg));
            } else {
                helper.setText(R.id.tvState, "已结束");
                stateView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.left_label_black_bg));
            }
            return;
        } catch (Exception e) {
            L.e(e.toString());
        }
        helper.setText(R.id.tvState, "未开始");
        stateView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.left_label_red_bg));

    }

}
